package com.scfire.util

import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.web.multipart.commons.CommonsMultipartFile

class PicFileUitl {

	public static String saveFile(String savePath, String directory, CommonsMultipartFile file, String extendName){
//		String rootPath = servletContext.getRealPath("/")
		//
		String filename = getFileName();
		String realname = savePath + directory + filename + extendName;
		try {
			FileOutputStream o = new FileOutputStream(realname);
			o.write(file.getBytes())
			o.close();
		} catch (FileNotFoundException e) {
			println ("file not found exception" + e.getMessage());
		} catch (IOException e) {
			println("writing file exception" + e.getMessage());
		}
		
		return directory + filename + extendName;
	}
	
	public static String getFileName(){
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
		String name = format.format(new Date())+ UUID.randomUUID().toString();
		return name;
	}
}
